<?php


namespace App\Helpers;



use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileUpload
{
    public function productImageUpload(Request $request,$type)
    {

        $file = $request->file('image');
        if ($request->hasFile('image') && $file->isValid()) {

            $folder = "goods";
            switch ($type){
                case 1:$folder = "goods";break;
                case 2:$folder = "details";break;
                case 3:$folder = "activities";break;
            }

            //判断文件夹是否已存在
            if(!Storage::disk('public')->exists($folder)){
                Storage::makeDirectory($folder);
            }



            $photo = $request->file('image');
            $store_result = $photo->store('/'.$folder,'public');
            $result = $store_result;
           return $result;


        }
        return[
            'message' => '文件不存在或文件上传出错！',
            'result' => 'failed'
        ];
    }

}
